package com.cz.nongyetongv5.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.bean.Photo;
import com.cz.nongyetongv5.custom.wheelview.Utils;
import com.cz.nongyetongv5.utils.CharsetUtil;
import com.cz.nongyetongv5.utils.ImageUtil;

import java.util.List;

public class PhotoGrideAdapter extends BaseAdapter {

    private List<Photo> photoGridData = null;

    private LayoutInflater layoutInflater = null;

    private OnClickListener clickListener = null;

    private Context context = null;

    public void setPhotoGridData(List<Photo> photoGridData) {
        this.photoGridData = photoGridData;
        this.notifyDataSetChanged();
    }

    public PhotoGrideAdapter(Context context, List<Photo> photoGridData,
                             LayoutInflater layoutInflater, OnClickListener clickListener) {
        this.photoGridData = photoGridData;
        this.layoutInflater = layoutInflater;

        this.context = context;
        this.clickListener = clickListener;

    }

    @Override
    public int getCount() {
        return photoGridData.size();
    }

    @Override
    public Object getItem(int position) {
        return photoGridData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewGroup iconLayout = null;

        if (convertView != null) {
            iconLayout = (ViewGroup) convertView;
        } else {
            iconLayout = (ViewGroup) layoutInflater.inflate(
                    R.layout.gridview_photo_item, null);
        }

        ImageButton imageButton = (ImageButton) iconLayout
                .findViewById(R.id.ivPhoto);


        imageButton.setOnClickListener(clickListener);


        if (photoGridData.get(position).getSmallPhotoPath() instanceof Integer) {
            imageButton.setImageResource((Integer) photoGridData.get(position)
                    .getSmallPhotoPath());

            iconLayout.findViewById(R.id.ivUploadSure).setVisibility(View.GONE);

        } else {
            String smallPhotoPath = (String) photoGridData.get(position)
                    .getSmallPhotoPath();

            if (CharsetUtil.isNotNull(smallPhotoPath)) {

                int widOrHei = Utils.dip2px(context, 80);

                Bitmap smallImage = ImageUtil.decodeSampledBitmapFromResource(
                        smallPhotoPath, widOrHei, widOrHei);

                // …Ë÷√Õº∆¨
                if (smallImage != null) {
                    imageButton.setImageBitmap(smallImage);
                }
            }

            if (photoGridData.get(position).isUpload()) {
                iconLayout.findViewById(R.id.ivUploadSure).setVisibility(View.VISIBLE);
            } else {
                iconLayout.findViewById(R.id.ivUploadSure).setVisibility(View.GONE);
            }
        }
        return iconLayout;
    }

}
